import java.util.Arrays;

public class Solution {
    public static void main(String[] args) {
        int[] nums = new int[]{1,2,3,4,5,6,7,0,9};
        moveZeroes(nums);
        for(int i  : nums) {
            System.out.print(i+" ");
        }
        int[] nums1 = new int[]{0,0,0,0,0,0,0};
        duplicateZeros(nums1);
    }
    //移动零
    public static void moveZeroes(int[] nums) {
        int slow = 0;
        int fast = 0;
        int n = nums.length;
        while(fast < n) {
            if(nums[fast] != 0) {
                int tmp = nums[fast];
                nums[fast] = nums[slow];
                nums[slow] = tmp;
                slow++;
            }
            fast++;
        }
    }
    //复写零
    public static void duplicateZeros(int[] arr) {
        int count = -1;
        int cur = 0;
        int n = arr.length;
        //找到最后一个需要复写的数的位置
        for(int i = 0; i < n; i++)  {
            if(arr[i] != 0) {
                count++;
            }else{
                count += 2;
            }
            if(count >= n-1) {
                cur = i;
                break;
            }
        }
        //处理最后一个0越界问题
        if(count == n) {
            arr[count-1] = 0;
            cur--;
            count-=2;
        }
        //从后向前复写
        for(int i = cur; i >= 0; i--) {
            if(arr[i] == 0) {
                arr[count--] = 0;
                arr[count--] = 0;
            }else{
                arr[count--] = arr[i];
            }
        }
    }
}
